package com.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wms.entity.WFactory;
import com.wms.dao.WFactoryDao;
import com.wms.entity.WUser;
import com.wms.mytool.Result;
import com.wms.service.WFactoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 厂家信息表(WFactory)表服务实现类
 *
 * @author makejava
 * @since 2023-10-17 19:45:24
 */
@Service("wFactoryService")
public class WFactoryServiceImpl implements WFactoryService {
    @Resource
    private WFactoryDao wFactoryDao;

    /**
     * 查询所有厂家信息
     * @return 实例对象
     */
    @Override
    public Result selectAllFactory() {
        List<WFactory> list = this.wFactoryDao.selectByMap(null);
        if(list!=null){
            return new Result(200,list,"查询成功");
        }else{
            return new Result(400,null,"查询失败");
        }
    }

    /**
     * 添加厂家
     * @param wFactory 厂家信息
     * @return 实例对象
     */
    @Override
    public Result addFactory(WFactory wFactory) {
        //根据商家名查询数据
        QueryWrapper<WFactory> qw = Wrappers.query();
        qw.eq("factory_name", wFactory.getFactoryName());
        WFactory factory = this.wFactoryDao.selectOne(qw);
        // 商家名存在
        if(factory!=null){
            return new Result(400,null,"厂家名已经存在");
        }else{
            // 商家名不存在，添加数据
            wFactory.setCreateTime(new Date());
            wFactory.setUpdateTime(new Date());
            int i = this.wFactoryDao.insert(wFactory);
            if(i==1){
                return new Result(200,i,"添加成功");
            }else{
                return new Result(400,i,"添加失败");
            }
        }

    }

    /**
     * 删除厂家信息
     * @param id 厂家id
     * @return 实例对象
     */
    @Override
    public Result deleteFactory(Integer id) {
        try{
            //根据厂家id删除数据
            QueryWrapper<WFactory> qw = Wrappers.query();
            qw.eq("factory_id", id);
            int i = this.wFactoryDao.delete(qw);
            if(i==1){
                return new Result(200,i,"删除成功");
            }else{
                return new Result(400,null,"删除失败");
            }
        }catch (Exception e){
            return new Result(400,null,"存在关联商品，不可删除");
        }

    }

    /**
     * 修改厂家信息
     * @param wFactory 厂家信息
     * @return 实例对象
     */
    @Override
    public Result updateFactory(WFactory wFactory) {
        wFactory.setUpdateTime(new Date());
        int i = this.wFactoryDao.updateById(wFactory);
        if(i==1){
            return new Result(200,i,"修改成功");
        }else{
            return new Result(400,null,"修改失败");
        }
    }

    public static void main(String[] args) {
        Date a = new Date();
        System.out.println(a);
    }
}
